//
//  LangSettingViewController.swift
//  worldexchange
//
//  Created by xionghao on 2019/1/10.
//  Copyright © 2019 xionghao. All rights reserved.
//

import UIKit
import QMUIKit

class LangSettingViewController: QMUICommonTableViewController {

    var langs:[LangItem] = [LangItem]()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.hidesBottomBarWhenPushed = true
        self.tableView.register(UINib(nibName: "LangTableViewCell", bundle: nil), forCellReuseIdentifier: "cell-item")
        
        self.refreshView()
        // Do any additional setup after loading the view.
    }
    
    func refreshView() {
        
        self.title = LanguageHelper.getString(key: "changeLang")
        let auto = LangItem()
        auto.name = LanguageHelper.getString(key: "SystemLang")
        
        let zh = LangItem()
        zh.name = "简体中文"
        zh.locale = "zh"
        
        
        let en = LangItem()
        en.name = "English"
        en.locale = "en"
        
        let th = LangItem()
        th.name = "ภาษาไทย"
        th.locale = "th"
        
        langs = [auto,zh,en,th]
        self.tableView.reloadData()
    }
    
    
   
    
    @objc func closeClick() {
        self.navigationController?.dismiss(animated: true, completion: {
            
        })
    }
    
    func reloadLang() {
        NotificationCenter.default.post(name: NSNotification.Name.init(LanguageChangeNotice), object: nil)
        refreshView()
    }
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        return langs.count
    }
    
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell-item") as! LangTableViewCell
       let userLang =  LanguageHelper.shareInstance.getUserLang()
        let item = langs[indexPath.row]
        if item.locale == userLang {
            cell.check.isHidden = false
        }else {
            cell.check.isHidden = true
        }
        return cell
    }
    
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let item = langs[indexPath.row]
        LanguageHelper.shareInstance.setLanguage(langeuage: item.locale)
        self.tableView.reloadRows(at: [indexPath], with: UITableView.RowAnimation.automatic)
        reloadLang()
    }


    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
